package com.front.web.wx.utils;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.redisUtils.JedisUtils;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.lvyou.domain.XcxConfig;
import com.bootdo.wx.domain.Config;

public class TokenUtils  {

	/**
	 *  得到通用token
	 * @param config
	 * @return
	 */
	public static String getToken(Config config) {
		String access_token = null;
		try {
			if(JedisUtils.exists("wx_access_token_"+config.getId())) {
				access_token = JedisUtils.get("wx_access_token_"+config.getId());
			}else {
				String result = HttpUtil
						.doGet("https://api.weixin.qq.com/cgi-bin/token"
								+ "?grant_type=client_credential&appid="
								+config.getWxAppId()+"&secret="+config.getWxAppSecret());
				 System.err.println("getTokenResult ->" + result);
				 access_token = JSONObject.parseObject(result).getString("access_token");
				 JedisUtils.set("wx_access_token_"+config.getId(), access_token, 7000);
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("token = "+access_token);
		return  access_token;
	}

	/**
	 *  得到通用lvyoutoken
	 * @param config
	 * @return
	 */
	public static String getLvyouToken(XcxConfig config) {
		String access_token = null;
		try {
			if(JedisUtils.exists("wx_access_token_"+config.getId())) {
				access_token = JedisUtils.get("wx_access_token_"+config.getId());
			}else {
				String result = HttpUtil
						.doGet("https://api.weixin.qq.com/cgi-bin/token"
								+ "?grant_type=client_credential&appid="
								+config.getAppId()+"&secret="+config.getAppSecret());
				System.err.println("getTokenResult ->" + result);
				access_token = JSONObject.parseObject(result).getString("access_token");
				JedisUtils.set("wx_access_token_"+config.getId(), access_token, 7000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("token = "+access_token);
		return  access_token;
	}



}
